package zerdlib;

import java.util.EventObject;
import java.util.Iterator;
import java.util.LinkedList;

import zerdlib.events.DefaultListenable;
import zerdlib.events.GenericListener;
import zerdlib.events.containers.ListChangeEvent;
import zerdlib.events.containers.ListenableLinkedList;

public class Test_ListenableLinkedList {
	ListenableLinkedList<ListenableString> test = new ListenableLinkedList<>();
	LinkedList<ListenableString> test2 = new LinkedList<>();
	
	
	private class ListenableString extends DefaultListenable {
		String str;
		
		public ListenableString(String str) {
			super();
			this.str = str;
		}
		
		@Override
		public String toString() {
			return str;
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof ListenableString) {
				return str.equals(((ListenableString) obj).str);
			}
			return false;
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test_ListenableLinkedList().test();
		
	}
	
	public void test() {
		test.addListener(new GenericListener() {
			@Override
			public void onEvent(EventObject ev_) {
				ListChangeEvent ev = (ListChangeEvent) ev_;
				switch(ev.getType()) {
				case ListChangeEvent.ELEMENT_ADDED :
					System.out.println("ELEMENT_ADDED "+ev.getIndex());
					break;
				case ListChangeEvent.ELEMENT_CHANGED :
					System.out.println("ELEMENT_CHANGED "+ev.getIndex());
					break;
				case ListChangeEvent.ELEMENT_REMOVED :
					System.out.println("ELEMENT_REMOVED "+ev.getIndex());
					break;
				case ListChangeEvent.INTERVAL_CHANGED :
					System.out.println("INTERVAL_CHANGED "+ev.getIndex());
					break;
					
				}
			}
		});
		
		add(0, new ListenableString("1")); // 1
		print();
		add(1, new ListenableString("2")); // 12
		print();
		add(1, new ListenableString("3")); // 132
		print();
		add(0, new ListenableString("4")); // 4132
		print();
		add(4, new ListenableString("5")); // 41325
		print();
		
		test.get(4).fireEvent(new EventObject(test.get(4)));
		test.get(0).fireEvent(new EventObject(test.get(0)));
		test.get(2).fireEvent(new EventObject(test.get(2)));
		
		set(4, new ListenableString("4bis"));
		print();
		set(0, new ListenableString("0bis"));
		print();
		set(1, new ListenableString("1bis"));
		print();
		
		remove(new ListenableString("3"));
		print();
		
		remove(3);
		print();
		remove(0);
		print();
		remove(1);
		print();
		
		
	}
	
	public void add(int i, ListenableString str) {
		test.add(i, str);
		test2.add(i, str);
	}
	
	public void set(int i, ListenableString str) {
		test.set(i, str);
		test2.set(i, str);
	}
	
	public void remove(int i) {
		test.remove(i);
		test2.remove(i);
	}
	
	public void remove(ListenableString s) {
		test.remove(s);
		test2.remove(s);
	}

	public void print() {
		Iterator<ListenableString> it = test.iterator();
		for(int i=0 ; i<test.size() ; i++) {
			System.out.println(test.get(i) + "\t" + test2.get(i) + "\t" + it.next());
		}
		System.out.println();
	}
}
